package com.hp.it.sonar.service.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import com.hp.it.pool.db.ConnectionPool;
import com.hp.it.sonar.bean.Project;
import com.hp.it.sonar.bean.Violation;
import com.hp.it.sonar.dao.PortalAccessor;
import com.hp.it.sonar.dao.ProjectAccessor;
import com.hp.it.sonar.dao.RuleAccessor;
import com.hp.it.sonar.dao.impl.PortalDataAccessor;
import com.hp.it.sonar.dao.impl.ProjectDataAccessor;
import com.hp.it.sonar.dao.impl.RuleDataAccessor;
import com.hp.it.sonar.service.IViolationService;

public class ViolationService implements IViolationService
{
	ProjectAccessor projectDao;

	RuleAccessor ruleDao;

	PortalAccessor portalDao;

	public ViolationService(ConnectionPool pool, String portalURL)
	{
		initizlize(pool, portalURL);
	}

	private void initizlize(ConnectionPool pool, String portalURL)
	{
		projectDao = new ProjectDataAccessor(pool);
		ruleDao = new RuleDataAccessor(pool);
		portalDao = new PortalDataAccessor(portalURL);
		portalDao.setProjectDao(projectDao);
		portalDao.setRuleDao(ruleDao);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.hp.it.sonar.service.impl.IViolationService#retrieveViolationSummary
	 * (java.lang.String, java.lang.String, int)
	 */
	public Map<String, String> retrieveViolationSummary(String groupId, String artifactId, int period)
	{
		return portalDao.retrieveViolationChangeSummary(groupId, artifactId, period);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.hp.it.sonar.service.impl.IViolationService#retrieveViolationDetails
	 * (java.lang.String, java.lang.String, int, java.lang.String)
	 */
	public Map<String, Collection<Violation>> retrieveViolationDetails(String groupId, String artifactId, int period,
			String violationPriority)
	{
		Map<String, Collection<Violation>> violationDetails = new HashMap<String, Collection<Violation>>();

		Collection<Violation> blockers = retrieveViolationDetailsByPriority(groupId, artifactId, period, "BLOCKER");
		if (blockers.size() != 0)
		{
			violationDetails.put("BLOCKER", blockers);
		}
		if (!"BLOCKER".equalsIgnoreCase(violationPriority))
		{
			Collection<Violation> criticals = retrieveViolationDetailsByPriority(groupId, artifactId, period,
					"CRITICAL");
			if (criticals.size() != 0)
			{
				violationDetails.put("CRITICAL", criticals);
			}
			if (!"CRITICAL".equalsIgnoreCase(violationPriority))
			{
				Collection<Violation> majors = retrieveViolationDetailsByPriority(groupId, artifactId, period, "MAJOR");
				if (majors.size() != 0)
				{
					violationDetails.put("MAJOR", majors);
				}
				if (!"MAJOR".equalsIgnoreCase(violationPriority))
				{
					Collection<Violation> minors = retrieveViolationDetailsByPriority(groupId, artifactId, period,
							"MINOR");
					if (minors.size() != 0)
					{
						violationDetails.put("MINOR", minors);
					}
					if (!"MINOR".equalsIgnoreCase(violationPriority))
					{
						Collection<Violation> infos = retrieveViolationDetailsByPriority(groupId, artifactId, period,
								"INFO");
						if (infos.size() != 0)
						{
							violationDetails.put("INFO", infos);
						}
					}
				}
			}
		}

		return violationDetails;
	}

	public Collection<Violation> retrieveViolationDetailsByPriority(String groupId, String artifactId, int period,
			String violationPriority)
	{
		Project project = projectDao.getProject(groupId, artifactId);
		Collection<Violation> violations = new ArrayList<Violation>();
		recursive(project, violations, period, violationPriority);
		return violations;
	}

	private void recursive(Project project, Collection<Violation> violations, int period, String violationPriority)
	{
		if (!"PRJ".equalsIgnoreCase(project.getScope()))
		{
			// Not a project , return
			return;
		}

		if ("BRC".equalsIgnoreCase(project.getQualifier()))
		{
			System.out.println(project.getLongName());
			// URL SEARCH
			portalDao.retrieveRecentChange(project, violations, period, violationPriority);
		} else if ("TRK".equalsIgnoreCase(project.getQualifier()))
		{
			Collection<Project> childs = projectDao.getProjectsByRootId(project.getId());

			boolean flag = false;
			for (Project p : childs)
			{
				if (!p.getScope().equalsIgnoreCase("PRJ"))
				{
					flag = true;
					break;
				}
			}
			if (flag)
			{
				portalDao.retrieveRecentChange(project, violations, period, violationPriority);
			} else
			{
				for (Project p : childs)
				{
					// TODO we can add some project filter here
					recursive(p, violations, period, violationPriority);
				}
			}
		}
	}
}
